import Vue from 'vue'
import Vuex from 'vuex'
import VueCookie from 'vue-cookie';
import {asyncRouterMap, constantRouterMap} from '../router/index';

Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        active: '',//菜单选中,主要针对F5刷新后菜单的默认选中
        meta_name: '',//当前页面标题
        loading: false,//加载效果
        upload: '/api/upload/image/',//上传图片地址
        upload_file: '/api/upload/file/',//上传文件地址
        routers: [],//权限筛选后的路由
        rules: [],//存储操作角色的权限
        detail: null,//存储所有内容的容器
    },
    getters: {
        detail: (state) => {
            return state.detail;
        },
        rules: (state) => {
            return state.rules;
        },
        active: (state) => {
            return state.active;
        },
        meta_name: (state) => {
            return state.meta_name;
        },
        loading: (state) => {
            return state.loading;
        },
        upload: (state) => {
            return state.upload;
        },
        upload_file: (state) => {
            return state.upload_file;
        },
        routers: (state) => {
            return state.routers;
        },
        authen: (state) => {
            return state.authen;
        },
    },
    mutations: {
        SET_DETAIL(state, param) {
            state.detail = param;
        },
        SET_RULES(state, param) {
            state.rules = param;
        },
        SET_ACTIVE(state, param) {
            state.active = param;
        },
        SET_META_NAME(state, param) {
            state.meta_name = param;
        },
        SET_LOADING(state, param) {
            state.loading = param;
        },
        SET_ROUTERS(state, param) {
            state.routers = param;
        },
        USER_LOGIN(state, param) {
            VueCookie.set('token', param.access_token, {expires: param.expires_in + 's'});
            VueCookie.set('token_type', param.token_type, {expires: param.expires_in + 's'});
            VueCookie.set('user', JSON.stringify(param.user), {expires: param.expires_in + 's'});
        },
        USER_LOGOUT() {
            VueCookie.delete('token');
            VueCookie.delete('token_type');
            VueCookie.delete('user');
        },
    },
    actions: {
        setDetail({commit}, param) {
            commit('SET_DETAIL', param);
        },
        setRules({commit}, param) {
            commit('SET_RULES', param);
        },
        //开启加载效果
        setLoading({commit}, param) {
            commit('SET_LOADING', param);
        },
        //设置菜单选中
        setActive({commit}, param) {
            commit('SET_ACTIVE', param);
        },
        //设置页面标题
        setMetaName({commit}, param) {
            commit('SET_META_NAME', param);
        },
        //登陆
        userLogin({commit}, param) {
            return new Promise((resolve) => {
                commit('USER_LOGIN', param);
                resolve();
            });
        },
        //退出登录
        userLogout({commit}) {
            return new Promise((resolve) => {
                commit('USER_LOGOUT');
                resolve();
            });
        },
        generateRoutes({commit}) {
            //全部执行完再返回
            return new Promise(resolve => {
                commit('SET_ROUTERS', asyncRouterMap);
                resolve();
            });
        },
    }
})
